/**
 * Created by billy on 2020/12/21.
 */

var wasteTime = 0;
var startTime = 0;
var lastTime = 0;
var _wasteStartTime = 0;

startTime = Date.now();

defineProperty(exports, "startTime", ()=>
{
	return startTime;
});

defineProperty(exports, "endTime", ()=>
{
	return endTime;
});

exports.toString = function ()
{
	lastTime = Date.now() - startTime - wasteTime;
	if (lastTime < 1000)
	{
		return lastTime + "ms";
	}
	else if (lastTime >= 1000 && lastTime < 60000)
	{
		return parseInt(lastTime / 10) / 100 + "s";
	}
	else
	{
		return g.time.formatTime(parseInt(lastTime / 1000), "hh:mm:ss")
	}
}

exports.format = function ($time)
{
	$time = trim($time);
	matchs = $time.match(/\d\d\d\d-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}/);
	if (matchs && matchs[0] == $time)
	{
		//YYYY-MM-DD hh:mm:ss
		return new Date($time).getTime();
	}

	//额外支持 YYYY-MM-DD、 MM-DD、 MM-DD hh:mm、hh:mm、hh:mm:ss
	var matchs = $time.match(/\d\d\d\d-\d{1,2}-\d{1,2}/);
	if (matchs && matchs[0] == $time)
	{
		//YYYY-MM-DD
		trace($time)
		return new Date($time + " 00:00:00").getTime();
	}

	matchs = $time.match(/\d{1,2}-\d{1,2}/);
	if (matchs && matchs[0] == $time)
	{
		//MM-DD
		var yyyy = new Date().getFullYear();
		return new Date(yyyy + "-" + $time + " 00:00:00").getTime();
	}

	matchs = $time.match(/\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}/);
	if (matchs && matchs[0] == $time)
	{
		//MM-DD hh:mm
		var yyyy = new Date().getFullYear();
		return new Date(yyyy + "-" + $time + ":00").getTime();
	}

	matchs = $time.match(/\d{1,2}:\d{1,2}/);
	if (matchs && matchs[0] == $time)
	{
		//hh:mm
		var d = new Date();
		return new Date(d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + $time + ":00").getTime();
	}

	matchs = $time.match(/\d{1,2}:\d{1,2}:\d{1,2}/);
	if (matchs && matchs[0] == $time)
	{
		//hh:mm:ss
		var d = new Date();
		return new Date(d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + $time).getTime();
	}

	return $time;
}

exports.pause = function ()
{
	_wasteStartTime = Date.now();
}

exports.resume = function ()
{
	wasteTime += Date.now() - _wasteStartTime;
}